<html>
  <head/>
  <body>
    
    <h1>Notes:</h1>
    <ol>
      <li>[EJ pg. 272] "The executor framework does for execution what the
      collections framework did for aggregation". In general avoid working directly
      with threads. Define work as tasks (callable or runnable) and run them using
      executor service. This also had the benefit of decoupling what to run (task)
      with how it gets run (executor service). 
      </li>
      <li>
      JDK 1.5 in java.util.concurrent Executor framework was introduced to
      execute tasks. This framework is much better than using threads in
      most cases. Go through Java executor tutorial at
      <a href="http://java.sun.com/docs/books/tutorial/essential/concurrency/executors.html">
      http://java.sun.com/docs/books/tutorial/essential/concurrency/executors.html
      </a> and also through item 68 of [EJ pg 271].
      </li>
      <li>
        A <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/Future.html">
        Future </a> represents the result of an asynchronous computation. 
        Methods are provided to check if the computation is complete, to wait
        for its completion, and to retrieve the result of the computation. 
        The result can only be retrieved using method get when the
        computation has completed, blocking if necessary until it is ready. 
        Cancellation is performed by the cancel method. Additional methods 
        are provided to determine if the task completed normally or was 
        cancelled. Once a computation has completed, the computation cannot be
        cancelled. If you would like to use a Future for the sake of
        cancellability but not provide a usable result, you can declare 
        types of the form Future<?> and return null as a result of the
        underlying task.  
      </li>
      <li>
        A <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/FutureTask.html">
        FutureTask </a> is a cancellable asynchronous computation. This class 
        provides a base implementation of Future, with methods to start and 
        cancel a computation, query to see if the computation is complete, and
         retrieve the result of the computation. The result can only be 
         retrieved when the computation has completed; the get  method will
        block if the computation has not yet completed. Once the computation
         has completed, the computation cannot be restarted or cancelled.
        <p/>
        A FutureTask can be used to wrap a Callable or Runnable object. 
        Because FutureTask implements Runnable, a FutureTask can be submitted
        to an Executor for execution.
        <p/>
        In addition to serving as a standalone class, this class provides 
        protected functionality that may be useful when creating customized 
        task classes. 
      </li>
      <li>
        A <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/Callable.html">
        callable</a> interface is similar to Runnable, in that both are designed
        for classes whose instances are potentially executed by another thread.
        A runnable however, does not return a result and cannot throw a checked
        exception. 
      </li>
      <li>
      <a target="_blank" href="http://java.sun.com/javase/7/docs/api/java/util/concurrent/Executor.html">
      Executor</a> executes submitted runnables.  
      <pre>
        Executor executor = anExecutor;
        executor.execute(new RunnableTask1());
      </pre>
      This interface decouples the task submission from the mechanism of how
      each task will be run. E.g. thread use, scheduling. 
      <p/>
      An executor can run the task synchronously or asynchronously. It can
      create a new thread for each task or have a thread pool. The executor
      below runs the submitted task immdeiately in the callers thread.
      <pre>
      class DirectExecutor implements Executor {
        public void execute(Runnable r) {
           r.run();
        }
      }      
      </pre>
      In the example below, the task is immediately run in a new thread.
      <pre>
      class ThreadPerTaskExecutor implements Executor {
       public void execute(Runnable r) {
         new Thread(r).start();
       }
      }      
      </pre>
      
      </li>
    </ol>
    
    <h1>References:</h1>
    <ol>
      <li>[EJ] Effective Java, second edition, by Joshua Bloch</li>
    </ol>
    
  </body>
</html>